package de.bazinga.rotw.menu;

import java.util.Iterator;

import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Spatial;
import com.jme.scene.shape.Quad;
import com.jme.scene.state.TextureState;
import com.jme.util.TextureManager;
import com.jmex.game.state.GameStateManager;

import de.bazinga.rotw.game.Game;
import de.bazinga.rotw.game.RotwGameState;
import de.bazinga.rotw.menu.item.MenuItem;
import de.bazinga.rotw.menu.objects.MovingBox;
import de.bazinga.rotw.statistics.OnlineStatistics;

public class MainMenu extends Menu {

	public MainMenu() {
		super("mainMenu");
	}
	
	protected void init() {		
		// Background
		Quad bgQuad = new Quad("bg",
				Game.getGame().getDisplay().getWidth(), 
				Game.getGame().getDisplay().getHeight());
		
		bgQuad.setRenderQueueMode(Renderer.QUEUE_ORTHO);   
	 	
		bgQuad.setLocalTranslation(new Vector3f(
				Game.getGame().getDisplay().getWidth()/2,
				Game.getGame().getDisplay().getHeight()/2,
				0));
		
		TextureState bg = Game.getGame().getDisplay().getRenderer().createTextureState();
		bg.setEnabled(true);
		bg.setTexture(TextureManager.loadTexture(RotwGameState.class
				.getClassLoader().getResource("background/mainMenu.png"),
				Texture.MinificationFilter.BilinearNearestMipMap,
				Texture.MagnificationFilter.Bilinear));
		
		bgQuad.setRenderState(bg);
		
		bgQuad.setLightCombineMode(Spatial.LightCombineMode.Off);
		bgQuad.updateRenderState();
		
		getRootNode().attachChild(bgQuad);
		
		
		MovingBox test = new MovingBox();
		test.setModelBound(new BoundingBox());
		test.updateModelBound();
		getRootNode().attachChild(test);
	}
	
	protected void initMenuItems() {
		/*menuItems.put("newGame", 
				new MenuItem("Neues Spiel",
						"newGame",
						ColorRGBA.black,
						2f,
						50,
						Game.getGame().getDisplay().getHeight() - 150));*/
		menuItems.put("newGame", 
				new MenuItem("newGame.png",
						"newGame",
						1f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight() - 150,
						250,100));
		
		
		
		menuItems.put("options", 
				new MenuItem("options.png",
						"options",
						2f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight()-250,
						250,100));
		
		
		menuItems.put("credits", 
				new MenuItem("credits.png",
						"credits",
						2f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight()-350,
						250,100));
		
		menuItems.put("exit", 
				new MenuItem("exit.png",
						"exit",
						2f,
						Game.getGame().getDisplay().getWidth() / 2.0f,
						Game.getGame().getDisplay().getHeight()-450,
						250,100));
	}
	
	protected void handleClick(String item) {
		if(menuItems.containsKey(item)) {
			if(item.equals("newGame")) {
				GameStateManager.getInstance().getChild("gameMenu").setActive(true);
				this.setActive(false);
				
			}else if(item.equals("exit")) {
				// Send Update to Online-Statistics
				OnlineStatistics.sendUpdate();
				
				Game.stopMenuSound();
				
				((RotwGameState)GameStateManager.getInstance().getChild("gameState")).disconnect();
				Game.getGame().shutdown();
				System.exit(0);
			}else if(item.equals("options")) {
				GameStateManager.getInstance().getChild("optionsMenu").setActive(true);
				this.setActive(false);
			}else if(item.equals("credits")) {
				GameStateManager.getInstance().getChild("creditsMenu").setActive(true);
				this.setActive(false);
			}
		}
	}
	
	protected void handleHover(MenuItem hoverItem) {
		Iterator<MenuItem> it = menuItems.values().iterator();
		while(it.hasNext()) {
			MenuItem item = it.next();
			if(hoverItem == item) {
				
				if(item  == menuItems.get("newGame")) {
					item.setText("newGameHover.png");
				}else if(item  == menuItems.get("options")) {
					item.setText("optionsHover.png");
				}else if(item  == menuItems.get("credits")) {
					item.setText("creditsHover.png");
				}else if(item  == menuItems.get("exit")) {
					item.setText("exitHover.png");
				}else{
					item.setTextColor(ColorRGBA.yellow);
				}
			}else{
				if(item  == menuItems.get("newGame")) {
					item.setText("newGame.png");
				}else if(item  == menuItems.get("options")) {
					item.setText("options.png");
				}else if(item  == menuItems.get("credits")) {
					item.setText("credits.png");
				}else if(item  == menuItems.get("exit")) {
					item.setText("exit.png");
				}else{
					item.setTextColor(ColorRGBA.black);
				}
			}
		}
	}
}